/**
 * 配置参考: https://cli.vuejs.org/zh/config/
 */

const HtmlWebpackPlugin = require("html-webpack-plugin");
var BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  .BundleAnalyzerPlugin;
//gzip(需要配合nginx配置)
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = ["js", "css"];

module.exports = {
  publicPath: process.env.NODE_ENV === "production" ? "/vue2study/" : "/",
  chainWebpack: (config) => {
    const svgRule = config.module.rule("svg");
    svgRule.uses.clear();
    svgRule
      .test(/\.svg$/)
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader");
    //开启代码分割
    config.optimization.splitChunks({
      chunks: "all", //async异步代码分割 initial同步代码分割 all同步异步分割都开启
      // minSize: 300000, //字节 引入的文件大于30kb才进行分割
      // maxSize: 300000, //50kb，尝试将大于50kb的文件拆分成n个50kb的文件
      cacheGroups: {
        // ElementUI: {
        //   name: "chunk-element-ui",
        //   test: /[\\/]node_modules[\\/]element-ui[\\/]/,
        //   chunks: "all",
        //   priority: 3,
        //   reuseExistingChunk: true,
        //   enforce: true,
        // },
        // PdfJsDist: {
        //   name: "chunk-pdf-js-dist",
        //   test: /[\\/]node_modules[\\/]pdf-js-dist[\\/]/,
        //   chunks: "all",
        //   priority: 3,
        //   reuseExistingChunk: true,
        //   enforce: true,
        // },
        // AgGridCommunity: {
        //   name: "chunk-ag-grid-community",
        //   test: /[\\/]node_modules[\\/]ag-grid-community[\\/]/,
        //   chunks: "all",
        //   priority: 3,
        //   reuseExistingChunk: true,
        //   enforce: true,
        // },
        // Echarts: {
        //   name: "chunk-echarts",
        //   test: /[\\/]node_modules[\\/]echarts[\\/]/,
        //   chunks: "all",
        //   priority: 3,
        //   reuseExistingChunk: true,
        //   enforce: true,
        // },
      },
    });
  },
  configureWebpack: {
    plugins: [
      new BundleAnalyzerPlugin({
        openAnalyzer: process.env.NODE_ENV === "production", // 在默认浏览器中是否自动打开报告，默认 true
      }),
      new CompressionWebpackPlugin({
        algorithm: "gzip",
        test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
        threshold: 1024,
        minRatio: 0.8,
      }),
      new HtmlWebpackPlugin({
        // ... 其他配置 ...
        inject: true, // 将脚本和样式表注入到 HTML 文件中
        // 添加 preload 提示
        template: "public/index.html", // 指定 HTML 模板
        preload: {
          rel: "preload", // 资源类型
          as: "script", // 预加载资源的类型（script, style, font, image, video, audio, document, fetch, track）
          include: "allChunks", // 对所有 chunks 都添加 preload 提示，也可以是一个函数，用于更细粒度的控制
        },
      }),
    ],
  },
  productionSourceMap: false,
  devServer: {
    open: true,
    port: 8001,
    overlay: {
      errors: true,
      warnings: true,
    },
    //解决跨域代理
    proxy: {
      "/admin": {
        target: "http://localhost:8081", // 目标远程服务器
        // target: "http://www.xxljunjun.com:8081",
        // target: "http://47.102.216.222:8081",
        ws: true, // 代理的WebSockets
        changeOrigin: true, // 允许改变“域”
      },
      "/user": {
        target: "http://localhost:8081", // 目标远程服务器
        // target: "http://www.xxljunjun.com:8081",
        // target: "http://47.102.216.222:8081",
        ws: true, // 代理的WebSockets
        changeOrigin: true, // 允许改变“域”
      },
    },
  },
};
